package com.hwd.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwd.gmall.product.mapper.BaseCategory1Mapper;
import com.hwd.gmall.model.product.BaseCategory1;
import com.hwd.gmall.product.service.BaseCategory1Service;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 一级分类的 service 实现类
 *
 * @author 黄伟东/Victor
 * @date 2022/4/17 18:56
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseCategory1ServiceImpl implements BaseCategory1Service {

    @Resource
    private BaseCategory1Mapper baseCategory1Mapper;

    /**
     * 根据id查询一级分类信息
     *
     * @param id 一级分类id
     * @return com.hwd.gmall.model.product.BaseCategory1
     */
    @Override
    public BaseCategory1 getById(Long id) {
        return baseCategory1Mapper.selectById(id);
    }

    /**
     * 查询全部一节分类
     *
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory1>
     */
    @Override
    public List<BaseCategory1> listCategory1s() {
        return baseCategory1Mapper.selectList(null);
    }

    /**
     * 新增一级分类数据
     *
     * @param baseCategory1 一级分类数据
     */
    @Override
    public void saveCategory1(BaseCategory1 baseCategory1) {
        int insert = baseCategory1Mapper.insert(baseCategory1);
        if (insert <= 0) {
            throw new RuntimeException("新增一级分类失败");
        }
    }

    /**
     * 删除一级分类数据
     *
     * @param id 一级分类数据
     */
    @Override
    public void deleteCategory1(Long id) {
        int delete = baseCategory1Mapper.deleteById(id);
        if (delete <= 0) {
            throw new RuntimeException("删除一级分类失败");
        }
    }

    /**
     * 修改一级分类表数据
     *
     * @param baseCategory1 一级分类表数据
     */
    @Override
    public void updateCategory1(BaseCategory1 baseCategory1) {
        int update = baseCategory1Mapper.updateById(baseCategory1);
        if (update <= 0) {
            throw new RuntimeException("修改一级分类失败");
        }
    }

    /**
     * 分页查询
     *
     * @param page 当前页数
     * @param size 每页显示数量
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory1>
     */
    @Override
    public IPage<BaseCategory1> page(Integer page, Integer size) {
        return baseCategory1Mapper.selectPage(new Page<>(page, size), null);
    }

    /**
     * 条件查询
     *
     * @param baseCategory1 查询条件
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory1>
     */
    @Override
    public List<BaseCategory1> search(BaseCategory1 baseCategory1) {
        LambdaQueryWrapper<BaseCategory1> wrapper = buildQueryParam(baseCategory1);
        return baseCategory1Mapper.selectList(wrapper);
    }

    /**
     * 分页条件查询
     *
     * @param baseCategory1 查询条件
     * @param page          当前页数
     * @param size          每页显示数量
     * @return java.util.List<com.hwd.gmall.model.product.BaseCategory1>
     */
    @Override
    public IPage<BaseCategory1> search(BaseCategory1 baseCategory1, Integer page, Integer size) {
        LambdaQueryWrapper<BaseCategory1> wrapper = buildQueryParam(baseCategory1);
        return baseCategory1Mapper.selectPage(new Page<>(page, size), wrapper);
    }

    /**
     * 查询条件构造
     *
     * @param baseCategory1 查询条件
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hwd.gmall.model.product.BaseCategory1>
     */
    private LambdaQueryWrapper<BaseCategory1> buildQueryParam(BaseCategory1 baseCategory1) {
        return new LambdaQueryWrapper<BaseCategory1>()
                .eq(Objects.nonNull(baseCategory1.getId()), BaseCategory1::getId, baseCategory1.getId())
                .like(StringUtils.isNotEmpty(baseCategory1.getName()), BaseCategory1::getName, baseCategory1.getName());
    }


}
